package com.fish.admin.module.auth.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fish.admin.module.auth.domain.dto.AuthInfoPageQueryDTO;
import com.fish.admin.module.auth.domain.entity.AuthInfo;
import com.fish.admin.module.auth.domain.vo.AuthInfoVO;
import com.fish.admin.module.auth.manager.AuthInfoManager;
import com.fish.admin.module.auth.service.AuthInfoService;
import com.fish.admin.module.auth.strategy.AuthStrategyManager;
import com.fish.common.core.annotation.tenant.IgnoreTenant;
import com.fish.common.core.base.AuthenticationRequest;
import com.fish.common.core.base.AuthenticationResponse;
import com.fish.common.core.domain.vo.PageVO;
import com.fish.common.core.enums.EnabledEnum;
import com.fish.common.core.enums.UserTypeEnum;
import com.fish.common.mybatisplus.util.PageUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class AuthInfoServiceImpl implements AuthInfoService {

    private final AuthStrategyManager authStrategyManager;

    private final AuthInfoManager authInfoManager;
    @Override
    @IgnoreTenant
    public AuthenticationResponse login(AuthenticationRequest authentication, UserTypeEnum userType) {

        return authStrategyManager.authStrategy(userType).authenticate(authentication, userType);
    }

    @Override
    public PageVO<AuthInfoVO> page(AuthInfoPageQueryDTO authInfoPageQueryDTO) {
        Page<AuthInfo> page = authInfoManager.page(authInfoPageQueryDTO);
        return PageUtils.toPage(page, (e -> {
            AuthInfoVO v = new AuthInfoVO();
            BeanUtils.copyProperties(e, v);
            return v;
        }));
    }

    @Override
    public void changeEnabled(Long id, EnabledEnum enabled) {
        AuthInfo authInfo = new AuthInfo();
        authInfo.setId(id);
        authInfo.setEnabled(enabled.getValue());
        authInfoManager.updateById(authInfo);
    }
}
